#include "stdafx.h"
#include <math.h>
#include <JeayeSDK/JVariant.h>

#ifndef _UNICODE
#include <wchar.h>
#endif

#ifdef _DEBUG
#define new new(__FILE__, __LINE__)
#endif

BOOL  c_variant_compare(c_variant_t *v1, c_variant_t *v2)
{
	if (v1->vt == v2->vt) {
		switch (v1->vt)
		{
		case C_VT_BOOL:
			return v1->v.un_bool == v2->v.un_bool ? TRUE : FALSE;
		case C_VT_CHAR:
			return v1->v.un_char == v2->v.un_char ? TRUE : FALSE;
		case C_VT_WCHAR:
			return v1->v.un_wchar == v2->v.un_wchar ? TRUE : FALSE;
		case C_VT_INT8:
			return v1->v.un_int8 == v2->v.un_int8 ? TRUE : FALSE;
		case C_VT_INT16:
			return v1->v.un_int16 == v2->v.un_int16 ? TRUE : FALSE;
		case C_VT_INT32:
			return v1->v.un_int32 == v2->v.un_int32 ? TRUE : FALSE;
		case C_VT_INT64:
			return v1->v.un_int64 == v2->v.un_int64 ? TRUE : FALSE;
		case C_VT_UINT8:
			return v1->v.un_uint8 == v2->v.un_uint8 ? TRUE : FALSE;
		case C_VT_UINT16:
			return v1->v.un_uint16 == v2->v.un_uint16 ? TRUE : FALSE;
		case C_VT_UINT32:
			return v1->v.un_uint32 == v2->v.un_uint32 ? TRUE : FALSE;
		case C_VT_UINT64:
			return v1->v.un_uint64 == v2->v.un_uint64 ? TRUE : FALSE;
		case C_VT_DOUBLE:
			return fabs(v1->v.un_double - v2->v.un_double) < 0.000001 ? TRUE : FALSE;
		case C_VT_FLOAT:
			return fabs(v1->v.un_float - v2->v.un_float) < 0.000001 ? TRUE : FALSE;
		case C_VT_CHAR_PTR:
			return _stricmp(v1->v.un_char_ptr, v2->v.un_char_ptr) == 0 ? TRUE : FALSE;
		case C_VT_WCHAR_PTR:
			return _wcsicmp(v1->v.un_wchar_ptr, v2->v.un_wchar_ptr) == 0 ? TRUE : FALSE;
		case C_VT_VOID_PTR:
			return v1->v.un_void_ptr == v2->v.un_void_ptr ? TRUE : FALSE;
		}
	}
	else {
		return memcmp(v1, v2, sizeof(c_variant_t)) == 0 ? TRUE : FALSE;
	}
	return FALSE;
}

JVariant::JVariant()
{
	memset(&m_var, 0, sizeof(m_var));
	m_var.vt = C_VT_EMPTY;
}

void* JVariant::get()
{
	switch (m_var.vt)
	{
	case C_VT_BOOL:
		return &m_var.v.un_bool;
	case C_VT_CHAR:
		return &m_var.v.un_char;
	case C_VT_WCHAR:
		return &m_var.v.un_wchar;
	case C_VT_INT8:
		return &m_var.v.un_int8;
	case C_VT_INT16:
		return &m_var.v.un_int16;
	case C_VT_INT32:
		return &m_var.v.un_int32;
	case C_VT_INT64:
		return &m_var.v.un_int64;
	case C_VT_UINT8:
		return &m_var.v.un_uint8;
	case C_VT_UINT16:
		return &m_var.v.un_uint16;
	case C_VT_UINT32:
		return &m_var.v.un_uint32;
	case C_VT_UINT64:
		return &m_var.v.un_uint64;
	case C_VT_DOUBLE:
		return &m_var.v.un_double;
	case C_VT_FLOAT:
		return &m_var.v.un_float;
	case C_VT_CHAR_PTR:
		return m_var.v.un_char_ptr;
	case C_VT_WCHAR_PTR:
		return m_var.v.un_wchar_ptr;
	case C_VT_VOID_PTR:
		return m_var.v.un_void_ptr;
	}
	return NULL;
}

void JVariant::set(bool val)
{
	m_var.vt = C_VT_BOOL;
	m_var.v.un_bool = val;
}

void JVariant::set(char val)
{
	if (m_var.vt != C_VT_EMPTY)
		memset(&m_var, 0, sizeof(m_var));

	m_var.vt = C_VT_CHAR;
	m_var.v.un_char = val;
}

void JVariant::set(wchar_t val)
{
	m_var.vt = C_VT_WCHAR;
	m_var.v.un_wchar = val;
}
void JVariant::set(INT8 val)
{
	m_var.vt = C_VT_INT8;
	m_var.v.un_int8 = val;
}

void JVariant::set(INT16 val)
{
	m_var.vt = C_VT_INT16;
	m_var.v.un_int16 = val;
}

void JVariant::set(INT32 val)
{
	m_var.vt = C_VT_INT32;
	m_var.v.un_int32 = val;
}

void JVariant::set(INT64 val)
{
	m_var.vt = C_VT_INT64;
	m_var.v.un_int64 = val;
}

void JVariant::set(UINT8 val)
{
	m_var.vt = C_VT_UINT8;
	m_var.v.un_uint8 = val;
}

void JVariant::set(UINT16 val)
{
	m_var.vt = C_VT_UINT16;
	m_var.v.un_uint16 = val;
}

void JVariant::set(UINT32 val)
{
	m_var.vt = C_VT_UINT32;
	m_var.v.un_uint32 = val;
}

void JVariant::set(UINT64 val)
{
	m_var.vt = C_VT_UINT64;
	m_var.v.un_uint64 = val;
}

void JVariant::set(float val)
{
	m_var.vt = C_VT_FLOAT;
	m_var.v.un_float = val;
}

void JVariant::set(double val)
{
	m_var.vt = C_VT_DOUBLE;
	m_var.v.un_double = val;
}

void JVariant::set(const char* val)
{
	m_var.vt = C_VT_CHAR_PTR;
	m_var.v.un_char_ptr = (char*)val;
}

void JVariant::set(const wchar_t* val)
{
	m_var.vt = C_VT_WCHAR_PTR;
	m_var.v.un_wchar_ptr = (wchar_t*)val;
}
